package com.hanter.android.radui.mvp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hanter.android.radui.R;

public abstract class MvpTitleBarFragment<V extends BaseView, T extends BasePresenter<V>>
        extends BaseFragment implements BaseView, View.OnClickListener {

    public T presenter;
    protected TitleBarHelper titleBarHelper;
    protected ContentLoadStatusViewHelper contentStatusViewHelper;
    private View rootView;
    private FrameLayout contentContainer;
    protected View contentView;
    private View customTitleView;
    private LoadingDialogHelper loadingDialogHelper;
    private boolean showLoading;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presenter = createPresenter();
        loadingDialogHelper = new LoadingDialogHelper(getChildFragmentManager());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_rad_mvp_with_title, container, false);
        initBaseContentView();
        initBaseViews();
        initTitleBar();
        return rootView;
    }

    private void initBaseViews() {
        titleBarHelper = new TitleBarHelper(rootView);
        titleBarHelper.leftBar.setOnClickListener(this);
        titleBarHelper.leftBarButton.setOnClickListener(this);
        titleBarHelper.rightBar.setOnClickListener(this);
        titleBarHelper.rightBarButton.setOnClickListener(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //noinspection unchecked
        presenter.attachView((V) this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    @Override
    public void onDestroyView() {
        if (presenter != null)
            presenter.detachView();
        disProgressDialog();
        super.onDestroyView();
    }

    protected void initTitleBar() {
        titleBarHelper.initTitleBar();
        titleBarHelper.leftBarButton.setVisibility(View.INVISIBLE);
    }

    /**
     * 加载框、无数据、主界面三种状态的处理
     */
    private void initBaseContentView() {
        contentContainer = rootView.findViewById(R.id.base_content_container);
        createAddContentView();
        contentStatusViewHelper = new ContentLoadStatusViewHelper(MvpTitleBarFragment.this, contentContainer, contentView);
        contentStatusViewHelper.initViews(showLoading);
    }

    /**
     * 主界面
     */
    private void createAddContentView() {
        contentView = LayoutInflater.from(getContext()).inflate(getLayout(), contentContainer, false);

        if (contentView != null) {
            contentContainer.addView(contentView);
            if (showLoading) {
                contentView.setVisibility(View.INVISIBLE);
            }
        }
    }

    protected abstract @LayoutRes int getLayout();

    protected abstract void initViews();

    protected void onContentRefreshClick() {

    }

    public abstract T createPresenter();

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.leftBarButton) {
            Activity activity = getActivity();
            if (activity != null) {
                activity.onBackPressed();
            }
        } else if (id == R.id.btnContentExceptionRefresh) {
            showLoadingView();
            onContentRefreshClick();
        }
    }

    /**
     * 必须在onCreate之前调用（否则不会显现加载框）
     *
     * @param showLoading 是否显示加载
     */
    public void setShowContentLoading(boolean showLoading) {
        this.showLoading = showLoading;
    }

    @Override
    public void showProgressDialog() {
        loadingDialogHelper.showProgressDialog();
    }

    @Override
    public void disProgressDialog() {
        loadingDialogHelper.dismissProgressDialog();
    }

    @Override
    public void hideContentView() {
        contentStatusViewHelper.hideContentView();
    }

    @Override
    public void showContentView() {
        contentStatusViewHelper.showContentView();
    }

    @Override
    public void showNoData() {
        contentStatusViewHelper.showNoDataView();
    }

    @Override
    public void hideNoData() {
        contentStatusViewHelper.hideExceptionView();
    }

    @Override
    public void showPageFault() {
        contentStatusViewHelper.showPageFaultView();
    }

    @Override
    public void showNoNetWork() {
        contentStatusViewHelper.showNoNetworkView();
    }

    @Override
    public void showLoadingView() {
        contentStatusViewHelper.showLoadingView();
    }

    @Override
    public void completeLoading() {
        contentStatusViewHelper.completeLoading();
    }

    @Override
    public void navBack() {
        requireActivity().onBackPressed();
    }

    public <W extends View> W findViewById(@IdRes int id) {
        if (rootView != null) {
            return rootView.findViewById(id);
        } else {
            return null;
        }
    }

    public void setColorPrimaryDark(int color) {
        titleBarHelper.compatPrimaryDark.setBackgroundColor(color);
    }

    public void setTitle(String title) {
        titleBarHelper.title.setText(title);
    }

    public void setTitle(@StringRes int resId) {
        titleBarHelper.title.setText(resId);
    }

    @SuppressWarnings("unused")
    public void setTitleBackground(int resId) {
        titleBarHelper.titleBar.setBackgroundResource(resId);
    }

    public void setColorPrimary(@ColorInt int color) {
        titleBarHelper.titleBar.setBackgroundColor(color);
    }

    public void setCustomTitleView(@LayoutRes int resId) {
        View titleView = LayoutInflater.from(getContext()).inflate(resId, titleBarHelper.realTitleBar, false);
        setCustomTitleView(titleView);
    }

    public void setCustomTitleView(View titleView) {
        if (customTitleView != null) {
            titleBarHelper.realTitleBar.removeView(customTitleView);
        }
        titleBarHelper.leftBar.setEnabled(false);
        titleBarHelper.realTitleBar.addView(titleView);
        customTitleView = titleView;
    }

    public void setRealShow(boolean show) {
        if (show) {
            titleBarHelper.titleBar.setVisibility(View.VISIBLE);
        } else {
            titleBarHelper.titleBar.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标题栏可见
     *
     * @param visibility visibility
     */

    public void setTitleBarVisibility(int visibility) {
        titleBarHelper.titleBar.setVisibility(visibility);
        titleBarHelper.titleBarLine.setVisibility(visibility);
    }


    public void setLeftTitleText(@NonNull String title) {
        titleBarHelper.leftBarButton.setText(title);
    }


    public void setLeftTitleText(@StringRes int resId) {
        titleBarHelper.leftBarButton.setText(resId);
    }


    public void setRightTitleText(@NonNull String title) {
        titleBarHelper.rightBarButton.setText(title);
    }


    public void setRightTitleTextColor(@ColorInt int color) {
        titleBarHelper.rightBarButton.setTextColor(color);
    }


    public void setRightTitleText(@StringRes int resId) {
        titleBarHelper.rightBarButton.setText(resId);
    }

    public void setLeftTitleImage(@DrawableRes int resId) {
        titleBarHelper.leftBarButton.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }


    public void setRightTitleImage(@DrawableRes int resId) {
        titleBarHelper.rightBarButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
    }

    public Button addRightButton(@NonNull String text) {
        return addRightButton(text, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public Button addRightButton(@NonNull String text, int width, int height) {
        Button button = new Button(getContext());
        button.setText(text);
        titleBarHelper.rightBar.addView(button, width, height);
        button.setMinWidth(0);
        button.setMinHeight(0);
        button.setMinimumHeight(0);
        button.setMinimumWidth(0);
        return button;
    }

}
